
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>TabBar · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="section8.html" />
    
    
    <link rel="prev" href="section6.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.1" data-path="../chapter1/">
            
                <a href="../chapter1/">
            
                    
                    Flutter vs React Native
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2" data-path="../chapter2/">
            
                <a href="../chapter2/">
            
                    
                    环境配置
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.2.1" data-path="../chapter2/section1.html">
            
                <a href="../chapter2/section1.html">
            
                    
                    前置依赖环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.2" data-path="../chapter2/section2.html">
            
                <a href="../chapter2/section2.html">
            
                    
                    配置 Android 环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.3" data-path="../chapter2/section3.html">
            
                <a href="../chapter2/section3.html">
            
                    
                    配置 Flutter 环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.2.4" data-path="../chapter2/section4.html">
            
                <a href="../chapter2/section4.html">
            
                    
                    创建并运行 Flutter 项目
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.3" data-path="./">
            
                <a href="./">
            
                    
                    核心Widget的使用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.3.1" data-path="section3.html">
            
                <a href="section3.html">
            
                    
                    MaterialApp
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.2" data-path="section1.html">
            
                <a href="section1.html">
            
                    
                    Text
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.3" data-path="section2.html">
            
                <a href="section2.html">
            
                    
                    Center
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.4" data-path="section4.html">
            
                <a href="section4.html">
            
                    
                    Scaffold
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.5" data-path="section5.html">
            
                <a href="section5.html">
            
                    
                    常用的布局组件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.6" data-path="section6.html">
            
                <a href="section6.html">
            
                    
                    AppBar
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.1.3.7" data-path="section7.html">
            
                <a href="section7.html">
            
                    
                    TabBar
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.8" data-path="section8.html">
            
                <a href="section8.html">
            
                    
                    TabController
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.9" data-path="section9.html">
            
                <a href="section9.html">
            
                    
                    TabBarView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.10" data-path="section10.html">
            
                <a href="section10.html">
            
                    
                    Drawer
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.11" data-path="section11.html">
            
                <a href="section11.html">
            
                    
                    UserAccountsDrawerHeader
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.12" data-path="section12.html">
            
                <a href="section12.html">
            
                    
                    ListView
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.3.13" data-path="section13.html">
            
                <a href="section13.html">
            
                    
                    CircleAvatar
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.4" data-path="../chapter4/">
            
                <a href="../chapter4/">
            
                    
                    自定义 Widget
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1.4.1" data-path="../chapter4/section1.html">
            
                <a href="../chapter4/section1.html">
            
                    
                    StatelessWidget
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.4.2" data-path="../chapter4/section2.html">
            
                <a href="../chapter4/section2.html">
            
                    
                    StatefulWidget
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.1.5" data-path="../chapter5/">
            
                <a href="../chapter5/">
            
                    
                    事件绑定
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.6" data-path="../chapter6/">
            
                <a href="../chapter6/">
            
                    
                    dio 数据请求
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.7" data-path="../chapter7/">
            
                <a href="../chapter7/">
            
                    
                    路由导航
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.8" data-path="../chapter8/">
            
                <a href="../chapter8/">
            
                    
                    修改 App 名称和图标
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.9" data-path="../chapter9/">
            
                <a href="../chapter9/">
            
                    
                    打包发布 Android 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.1.10" data-path="../chapter10/">
            
                <a href="../chapter10/">
            
                    
                    项目中用到的资源
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >TabBar</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="tabbar">TabBar</h1>
<p>&#x5728;&#x9875;&#x9762;&#x4E0A;&#x6E32;&#x67D3; <code>TabBar</code> &#x7EC4;&#x4EF6;&#xFF0C;&#x901A;&#x5E38;&#x914D;&#x5408; <code>Scaffold</code> &#x7EC4;&#x4EF6;&#x7684; <code>bottomNavigationBar</code> &#x5C5E;&#x6027;&#x4E00;&#x8D77;&#x4F7F;&#x7528;&#xFF0C;&#x7528;&#x6765;&#x6E32;&#x67D3;&#x5E95;&#x90E8;&#x7684; TabBar &#x6548;&#x679C;&#x3002;</p>
<p>&#x6CE8;&#x610F;&#xFF1A;</p>
<ol>
<li><p>TabBar &#x6700;&#x597D;&#x548C;&#x6709;&#x72B6;&#x6001;&#x9875;&#x9762;&#x914D;&#x5408;&#x4F7F;&#x7528;</p>
</li>
<li><p>TabBar &#x5FC5;&#x987B;&#x6307;&#x5B9A; <code>TabController</code> &#x63A7;&#x5236;&#x5668;&#xFF0C;&#x7528;&#x6765;&#x63A7;&#x5236; TabBar &#x7684;&#x5207;&#x6362;</p>
</li>
<li><p>&#x5982;&#x82E5;&#x4E0D;&#x6307;&#x5B9A; <code>TabController</code> &#x63A7;&#x5236;&#x5668;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>DefaultTabController</code> &#x7EC4;&#x4EF6;&#xFF0C;&#x628A; <code>TabBar</code> &#x7EC4;&#x4EF6;&#x5305;&#x88F9;&#x8D77;&#x6765;&#xFF0C;&#x540C;&#x65F6;&#x63D0;&#x4F9B;&#x9700;&#x8981;&#x5207;&#x6362;&#x7684;&#x9875;&#x9762;&#x4E2A;&#x6570;&#x5373;&#x53EF;&#xFF0C;&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-dart">DefaultTabController(
  <span class="hljs-comment">// &#x6307;&#x5B9A;&#x9700;&#x8981;&#x5207;&#x6362;&#x7684;&#x9875;&#x9762;&#x4E2A;&#x6570;</span>
  length: tablist.length,
  child: Scaffold(
    appBar: AppBar(),
    <span class="hljs-comment">// &#x88AB; tabbar &#x63A7;&#x5236;&#x5207;&#x6362;&#x7684;&#x9875;&#x9762;</span>
    body: TabBarView(),
    <span class="hljs-comment">// &#x6307;&#x5B9A; tab &#x9879;</span>
    bottomNavigationBar: TabBar(
      tabs: tablist
    )
  ),
)
</code></pre>
</li>
<li><p>TabController &#x5FC5;&#x987B;&#x7528;&#x5728;&#x62E5;&#x6709; <code>TickerProviderStateMixin</code> &#x6216; <code>SingleTickerProviderStateMixin</code> &#x7279;&#x5F81;&#x7684;&#x7C7B;&#x4E2D;&#xFF0C;&#x56E0;&#x6B64;&#xFF0C;&#x5FC5;&#x987B;&#x8BA9; TabController &#x6240;&#x5728;&#x7684;&#x7C7B;&#x5B9E;&#x73B0;&#x6B64;&#x7279;&#x5F81;&#xFF0C;&#x4EE3;&#x7801;&#x793A;&#x4F8B;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-dart"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">_HomePageState</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">State</span>&lt;<span class="hljs-title">HomePage</span>&gt; <span class="hljs-title">with</span> <span class="hljs-title">SingleTickerProviderStateMixin</span> </span>{ }
</code></pre>
</li>
</ol>
<h2 id="&#x5B9E;&#x73B0;&#x6B65;&#x9AA4;&#xFF1A;">&#x5B9E;&#x73B0;&#x6B65;&#x9AA4;&#xFF1A;</h2>
<ol>
<li><p>&#x521B;&#x5EFA;&#x5BF9;&#x5E94;&#x7684; Tab &#x9879;&#xFF1A;</p>
<pre><code class="lang-dart"><span class="hljs-built_in">List</span>&lt;Widget&gt; tablist = &lt;Widget&gt;[
  Tab(
    text: <span class="hljs-string">&apos;&#x6B63;&#x5728;&#x70ED;&#x6620;&apos;</span>,
    icon: Icon(Icons.movie),
  ),
  Tab(
    text: <span class="hljs-string">&apos;&#x5373;&#x5C06;&#x4E0A;&#x6620;&apos;</span>,
    icon: Icon(Icons.movie_filter),
  ),
  Tab(
    text: <span class="hljs-string">&apos;Top250&apos;</span>,
    icon: Icon(Icons.local_movies),
  ),
];
</code></pre>
</li>
<li><p>&#x521B;&#x5EFA; TabController &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-dart">TabController tabCtrl;
</code></pre>
</li>
<li><p>&#x5728; initState &#x751F;&#x547D;&#x5468;&#x671F;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x521D;&#x59CB;&#x5316; <code>tabCtrl</code> &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-dart"><span class="hljs-meta">@override</span>
<span class="hljs-keyword">void</span> initState() {
  <span class="hljs-keyword">super</span>.initState();
  tabCtrl = TabController(vsync: <span class="hljs-keyword">this</span>, length: tablist.length);
}
</code></pre>
</li>
<li><p>&#x4E3A; Scaffold &#x6DFB;&#x52A0; <code>bottomNavigationBar</code> &#x9009;&#x9879;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code class="lang-dart">bottomNavigationBar: Container(
  <span class="hljs-comment">// &#x8BBE;&#x7F6E; tabbar &#x7684;&#x80CC;&#x666F;&#x8272;</span>
  decoration: BoxDecoration(color: Colors.black),
  <span class="hljs-comment">// &#x8BBE;&#x7F6E; tabbar &#x7684;&#x9AD8;&#x5EA6;</span>
  height: <span class="hljs-number">50</span>,
  <span class="hljs-comment">// &#x6307;&#x5B9A; tabbar &#x7EC4;&#x4EF6;</span>
  child: TabBar(
    <span class="hljs-comment">// &#x6307;&#x5B9A; tabbar &#x7684;&#x63A7;&#x5236;&#x5668;&#xFF0C;&#x5FC5;&#x987B;&#x63D0;&#x4F9B;&#x63A7;&#x5236;&#x5668;&#xFF0C;&#x5426;&#x5219; tabbar &#x4E0D;&#x80FD;&#x6B63;&#x5E38;&#x5DE5;&#x4F5C;</span>
    controller: tabCtrl,
    <span class="hljs-comment">// &#x8BBE;&#x7F6E; tabbar &#x4E2D;&#x6587;&#x672C;&#x7684;&#x6837;&#x5F0F;&#xFF0C;&#x6CE8;&#x610F;&#xFF0C;height &#x5C5E;&#x6027;&#x5F88;&#x91CD;&#x8981;&#xFF0C;&#x5FC5;&#x987B;&#x8BBE;&#x7F6E;&#x4E3A; 0&#xFF0C;&#x5426;&#x5219;&#x6587;&#x672C;&#x548C;&#x56FE;&#x6807;&#x4E4B;&#x95F4;&#x7684;&#x8DDD;&#x79BB;&#x592A;&#x5927;&#xFF0C;&#x4E0D;&#x7F8E;&#x89C2;</span>
    labelStyle: TextStyle(height: <span class="hljs-number">0</span>, fontSize: <span class="hljs-number">10</span>),
    <span class="hljs-comment">// &#x8BBE;&#x7F6E;&#x6307;&#x793A;&#x5668;&#x7684;&#x989C;&#x8272;</span>
    indicatorColor: Colors.red,
    <span class="hljs-comment">// &#x6307;&#x5B9A;&#x5F53;&#x524D; tabbar &#x603B;&#x5171;&#x6709;&#x51E0;&#x4E2A;&#x6309;&#x94AE;</span>
    tabs: tablist,
  ),
)
</code></pre>
</li>
</ol>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="section6.html" class="navigation navigation-prev " aria-label="Previous page: AppBar">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="section8.html" class="navigation navigation-next " aria-label="Next page: TabController">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"TabBar","level":"1.1.3.7","depth":3,"next":{"title":"TabController","level":"1.1.3.8","depth":3,"path":"chapter3/section8.md","ref":"chapter3/section8.md","articles":[]},"previous":{"title":"AppBar","level":"1.1.3.6","depth":3,"path":"chapter3/section6.md","ref":"chapter3/section6.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter3/section7.md","mtime":"2018-12-12T15:09:00.029Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-12-17T10:25:33.898Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

